<script>
function sortJsonMap(data){
	var values = [];
	var keys = [];
	for(var i in data) {
	   keys.push(i);
	   values.push(data[i]);                     
	}
	values.sort();  
	var map = new Object();
	for(var i in values) {
	     map[keys[i]]=values[i];
	}
	return map;
} 

$(document).ready(function() {
	var $pays_re = $('#paysRecherche');
	var $ville_re = $('#villeRecherche');
	$pays_re.on('change', function() {
		var val = $(this).val(); 
		if(val != '') {
			$ville_re.empty(); // on vide la liste 
			$.ajax({
				url: '<?php echo base_url();?>index.php/dn/selectville',
				data: 'id_pays='+ val, // on envoie $_GET['id_etat_type']
				dataType: 'json',
				success: function(data) {
					/*for (var i in json) {
					   for (var j in json[i]) {
					     console.log(json[i][j]);
					   }
					}*/
					json = sortJsonMap(data);
					$.each(json, function(index, value) {
						$ville_re.append('<option value="'+ index +'">'+  value +'</option>');
					});
				}
			});
		}
	});
});
</script>
